package com.ants.boot.wx.service.impl;

import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.wx.dao.WxSuggestMapper;
import com.ants.boot.wx.domain.WxSuggest;
import com.ants.boot.wx.dto.wxsuggest.AddWxSuggestDto;
import com.ants.boot.wx.dto.wxsuggest.QueryWxSuggestDto;
import com.ants.boot.wx.service.WxSuggestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 蚂蚁会花呗
 * @date 2022/6/1 10:44
 */
@Service
public class WxSuggestServiceImpl extends ServiceImpl<WxSuggestMapper, WxSuggest> implements WxSuggestService {


    @Override
    public IPage<WxSuggest> pageInfo(QueryWxSuggestDto dto) {
        QueryWrapper<WxSuggest> queryWrapper = new QueryWrapper<>();
        Page<WxSuggest> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        if (StringUtils.isNotBlank(dto.getType())){
            queryWrapper.lambda()
                    .eq(WxSuggest::getType,dto.getType());
        }
        if (StringUtils.isNotBlank(dto.getPhone())){
            queryWrapper.and(wrapper->wrapper.lambda().like(WxSuggest::getPhone,dto.getPhone()));
        }
        return this.page(page,queryWrapper);
    }

    @Override
    @Transactional
    public boolean addSuggest(AddWxSuggestDto dto) {
        WxSuggest wxSuggest = new WxSuggest();
        BeanUtils.copyProperties(dto,wxSuggest);
        wxSuggest.setUserId(SecurityUtils.getLoginUser().getUserId());
        return this.save(wxSuggest);
    }

    @Override
    @Transactional
    public boolean delSuggest(List<Integer> ids) {
        if (ids.isEmpty()){
            return false;
        }
        return this.removeByIds(ids);
    }
}
